package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CarInfo;
import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.Storages;
import cn.wolfcode.crm.query.StoragesQueryObject;
import cn.wolfcode.crm.service.IStoragesService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("storages")
public class StoragesController {

    @Autowired
    IStoragesService storagesService;

    @RequestMapping("")
    public String index(StoragesQueryObject qo) {
        //请求的分发
        if (qo.getState() == 0) {
            return "storages/list";

        } else if (qo.getState() == 1) {
            return "statePay/list";

        } else if (qo.getState() == 2) {
            return "stateBring/list";
        } else if (qo.getState() == 3) {
            return "stateUpdate/list";
        } else if (qo.getState() == 4) {
            return "stateNopay/list";
        }else if (qo.getState() == 5) {
            return "stateTnt/list";
        }
        return "storages/list";
    }



    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("storages:list")
    @PermissionName("保单列表")
    public PageResult list(StoragesQueryObject qo) {
        return storagesService.query(qo);
    }

    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("storages:save")
    @PermissionName("保单保存")
    public JsonResult save(Storages record) {
        JsonResult result = new JsonResult();
        try {
            storagesService.insert(record);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("保存操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("storages:update")
    @PermissionName("保单更新")
    public JsonResult update(Storages record) {
        JsonResult result = new JsonResult();
        try {
            storagesService.updateByPrimaryKey(record);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("更新操作操作失败");
        }
        return result;
    }

    @RequestMapping("saveCar")
    @ResponseBody
    @RequiresPermissions("storages:saveCar")
    @PermissionName("保单获取车辆数据")
    public JsonResult saveCar(CarInfo carInfo, HttpSession session) {
        JsonResult result = new JsonResult();
        try {
            session.setAttribute("info", carInfo);
            System.out.println("1" + carInfo);
            result.setMsg("第1次");
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("保存车辆信息");
        }
        return result;
    }

    @RequestMapping("saveClient")
    @ResponseBody
    @RequiresPermissions("storages:saveClient")
    @PermissionName("保单获取客户数据")
    public JsonResult saveClient(Client ct, HttpSession session) {
        JsonResult result = new JsonResult();
        try {
            session.setAttribute("client", ct);
            System.out.println("2" + ct);
            result.setMsg("第2次");
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("保存车辆信息");
        }
        return result;
    }

    @RequestMapping("saveSafe")
    @ResponseBody
    @RequiresPermissions("storages:saveSafe")
    @PermissionName("保单获取机构和产品数据")
    public JsonResult saveSafe(Long storages_id,Long agency_id, Long product_id, Double limit, Double durations, HttpSession session) {
        JsonResult result = new JsonResult();
        try {
            Client client = (Client) session.getAttribute("client");
            CarInfo info = (CarInfo) session.getAttribute("info");
            storagesService.saveStorages(storages_id,agency_id, product_id, limit, durations, client, info);
            result.setMsg("第三次");
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("保存保险信息");
        }
        return result;
    }

    @RequestMapping("storagesIdsStoraId")
    @ResponseBody
    @RequiresPermissions("storages:storagesIdsStoraId")
    @PermissionName("保单修改状态")
    public void storagesIdsStoraId(Long id, Integer state, Integer pay) {
        //修改state状态
        storagesService.storagesIdsStoraId(id,state,pay);

    }
    @RequestMapping("storagesIdsStora")
    @ResponseBody
    @RequiresPermissions("storages:storagesIdsStora")
    @PermissionName("保单保存时间和缴费方式")
    public void storagesIdsStora(Storages storages) {
        //修改state状态
        storagesService.storagesIdsStora(storages);
    }
    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions("storages:delete")
    @PermissionName("保单删除数据")
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            //修改state状态
            storagesService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("拒保失败");
        }
        return result;
    }


}
